package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.IAdChannelService;
import com.heima.model.admin.dtos.AdChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelService extends ServiceImpl<AdChannelMapper, AdChannel> implements IAdChannelService {

    /**
     * 查询频道列表
     * @param adChannelDTO
     * @return
     */
    @Override
    public ResponseResult list(AdChannelDTO adChannelDTO) {
        //1.校验参数
        if(adChannelDTO == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
            //分页数据校验
        adChannelDTO.checkParam();
        //2.按照名称模糊分页查询
        IPage<AdChannel> pageQuery = new Page<>(adChannelDTO.getPage(), adChannelDTO.getSize());
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        if(StringUtils.isNotBlank(adChannelDTO.getName())){
            queryWrapper.like(AdChannel::getName, adChannelDTO);
        }
            //按照 ord 字段正序排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(pageQuery, queryWrapper);
        //3.响应结果
        PageResponseResult pageResponseResult = new PageResponseResult(adChannelDTO.getPage(), adChannelDTO.getSize(), pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //校验参数
            //频道名称不能为空
        if(adChannel == null || StringUtils.isBlank(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
            //频道不能重复
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdChannel::getName, adChannel.getName());
        int count = this.count(queryWrapper);
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已存在");
        }
            //频道名称不能大于10个字符
        if(adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "频道名称不能超过 10 个字符");
        }
        //保存数据
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //响应结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.校验参数
        if(adChannel == null || adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel oldChannel = this.getById(adChannel);
        if(oldChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道信息不存在");
        }
            //频道名称不能重复
        if(StringUtils.isNotBlank(adChannel.getName()) && !oldChannel.getName().equals(adChannel.getName())){
            if(adChannel.getName().length() > 10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能超过 10 个字符");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已存在");
            }
        }
        //2.修改数据
        this.updateById(adChannel);
        //3.响应结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.校验参数
            //频道 id 不能为空
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
            //频道状态为有效时，不能删除
        AdChannel adChannel = this.getById(id);
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "有效状态的频道信息不能删除");
        }
        //2.删除频道
        this.removeById(id);
        //3.响应结果
        return ResponseResult.okResult();
    }

    /**
     * leadnews-article 微服务处理文章发布-根据 id 查询频道信息
     * @param channelId
     * @return
     */
    @Override
    public ResponseResult<AdChannel> findChannelById(Integer channelId) {
        if(channelId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR, "频道 id 不能为空");
        }
        AdChannel adChannel = this.getById(channelId);
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR, "频道信息不存在");
        }
        return ResponseResult.okResult(adChannel);
    }
}
